<template>
  <view class="container">
    <!-- 加载状态 -->
    <view v-if="loading" class="loading-container">
      <text>加载中...</text>
    </view>

    <!-- 公告详情 -->
    <view v-else-if="noticeDetail" class="notice-detail">
      <view class="notice-content">
        <!-- 公告头部信息 -->
        <view class="notice-header">
          <text class="notice-title">{{ noticeDetail.title }}</text>
          <view class="notice-meta">
            <text class="notice-time">发布时间：{{ formatDateTime(noticeDetail.createTime) }}</text>
            <text class="notice-views">查看次数：{{ noticeDetail.viewCount }}</text>
          </view>
        </view>

        <!-- 公告正文 -->
        <view class="notice-body">
          <text class="notice-text">{{ noticeDetail.content }}</text>
        </view>
      </view>
    </view>

    <!-- 错误状态 -->
    <view v-else class="error-container">
      <text>公告不存在或已删除</text>
      <button @click="goBack" class="back-btn">返回</button>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import { getNoticeDetail } from '@/api/notice';

const loading = ref(true);
const noticeDetail = ref(null);
const noticeId = ref(null);

// 格式化日期时间
const formatDateTime = (dateTime) => {
  if (!dateTime) return '';
  const date = new Date(dateTime);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  const hours = String(date.getHours()).padStart(2, '0');
  const minutes = String(date.getMinutes()).padStart(2, '0');
  return `${year}-${month}-${day} ${hours}:${minutes}`;
};

// 返回上一页
const goBack = () => {
  uni.navigateBack();
};

// 获取公告详情
const fetchNoticeDetail = async () => {
  if (!noticeId.value) {
    loading.value = false;
    return;
  }

  try {
    const response = await getNoticeDetail(noticeId.value);
    noticeDetail.value = response.data;
  } catch (error) {
    console.error('获取公告详情失败:', error);
    noticeDetail.value = null;
  } finally {
    loading.value = false;
  }
};

// 页面加载时获取参数
onLoad((options) => {
  if (options.id) {
    noticeId.value = options.id;
    fetchNoticeDetail();
  } else {
    loading.value = false;
  }
});
</script>

<style lang="scss" scoped>
.container {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.loading-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200rpx;
  font-size: 28rpx;
  color: #666;
}

.notice-detail {
  padding: 20rpx;
}

.notice-content {
  background: #fff;
  border-radius: 16rpx;
  padding: 32rpx;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.04);
}

.notice-header {
  margin-bottom: 32rpx;
  padding-bottom: 24rpx;
  border-bottom: 1rpx solid #f0f0f0;
}

.notice-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
  line-height: 1.4;
  display: block;
  margin-bottom: 16rpx;
}

.notice-meta {
  display: flex;
  flex-direction: column;
  gap: 8rpx;
}

.notice-time,
.notice-views {
  font-size: 24rpx;
  color: #999;
}

.notice-body {
  line-height: 1.6;
}

.notice-text {
  font-size: 30rpx;
  color: #333;
  white-space: pre-wrap;
  word-break: break-word;
}

.error-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 400rpx;
  font-size: 28rpx;
  color: #666;
}

.back-btn {
  margin-top: 32rpx;
  padding: 16rpx 32rpx;
  background-color: #007aff;
  color: #fff;
  border: none;
  border-radius: 8rpx;
  font-size: 28rpx;
}
</style>